package com.ddxz.tool.web.dto;

import com.ddxz.tool.core.common.DdxzStatus;
import com.ddxz.tool.core.convert.CollectionUtil;
import com.github.pagehelper.Page;
import lombok.Data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author user1
 * @date 2019-12-23
 * @version 1.0.0
 */
@Data
public class PageResponse {
    private Long totalElements;
    
    private Integer totalPage;
    
    private List<? extends Object> list;

    public PageResponse(int size) {
        list = new ArrayList<>(size);
    }

    public static <S> PageResponse getInstance(Page<S> src, Class<?> tar, int type) {
        PageResponse response = new PageResponse(src.getResult().size());
        response.setTotalElements(src.getTotal());
        response.setTotalPage(src.getPages());
        CollectionUtil.copyCollection(src.getResult(), (Collection<Object>) response.getList(), tar, type);
        return response;
    }

    public static <T> PageResponse getInstance(Class<T> tar) {
        PageResponse response = new PageResponse(0);
        response.totalElements = 0L;
        response.totalPage = 1;

        return response;
    }

    public static <S> PageResponse getInstance(Page<S> src, Class<?> tar) {
        PageResponse response = new PageResponse(src.getResult().size());
        response.setTotalElements(src.getTotal());
        response.setTotalPage(src.getPages());
        CollectionUtil.copyCollection(src.getResult(), (Collection<Object>) response.getList(), tar);

        return response;
    }

    private static final int OK_CODE = 200;
    private static final String OK_MESSAGE = "SUCCESS";
}
